package de.dlrg.wms.gui.forms;

import java.util.ArrayList;
import java.util.Observable;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import de.dlrg.wms.controller.controllers.AgeClassController;
import de.dlrg.wms.gui.forms.generic.GenericForm;
import de.dlrg.wms.model.AgeClass;
import de.dlrg.wms.model.Discipline;
import org.eclipse.swt.widgets.Table;

public class AgeClassForm extends GenericForm {
	private java.util.List<Discipline> disciplines;
	private Text txtNameofageclass;
	private Text txtCalculationformula;
	private AgeClass ageClassToWorkOn;
	private AgeClassController ageClassController;

	private Table availableDiscipline;
	private Table selectedDiscipline;

	public AgeClassForm(Composite parent, int style,
			AgeClassController ageClassController) {
		this(parent, style, ageClassController, new AgeClass());

	}

	private void createDisciplineTableItem(Discipline discipline, Table parent) {
		TableItem tableItem = new TableItem(parent, 0);
		tableItem.setText(discipline.getName());
		tableItem.setData(discipline);
	}

	protected void initData() {

		this.txtCalculationformula.setText(ageClassToWorkOn
				.getCalculationFormula());
		this.txtNameofageclass.setText(ageClassToWorkOn.getName());
		this.disciplines = this.ageClassController.getDisciplineController()
				.readAll();
		for (Discipline discipline : disciplines) {
			if (this.ageClassToWorkOn.getDiscipline().contains(discipline)) {
				continue;
			}
			this.createDisciplineTableItem(discipline, this.availableDiscipline);
		}

		for (Discipline discipline : this.ageClassToWorkOn.getDiscipline()) {
			this.createDisciplineTableItem(discipline, selectedDiscipline);
		}
	}

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 * @wbp.parser.constructor
	 */
	public AgeClassForm(Composite parent, int style,
			final AgeClassController ageClassController,
			final AgeClass ageClassToWorkOn) {
		super(parent, style);
		{
			this.ageClassController = ageClassController;
			this.ageClassToWorkOn = ageClassToWorkOn;
		}
		this.center.setLayout(new GridLayout(2, false));

		Label lblAgeClassName = new Label(this.center, SWT.NONE);
		lblAgeClassName.setText("Age Class Name");

		txtNameofageclass = new Text(this.center, SWT.BORDER);
		txtNameofageclass.setText("nameOfAgeClass");
		txtNameofageclass.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
				true, false, 1, 1));

		Label label = new Label(this.center, SWT.NONE);
		label.setText("Calculation Formula");

		txtCalculationformula = new Text(this.center, SWT.BORDER);
		txtCalculationformula.setText("calculationFormula");
		txtCalculationformula.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
				true, false, 1, 1));

		Group group = new Group(this.center, SWT.NONE);
		group.setLayout(new GridLayout(3, false));
		group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));

		availableDiscipline = new Table(group, SWT.BORDER | SWT.FULL_SELECTION);
		availableDiscipline.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
				true, true, 1, 1));
		availableDiscipline.setHeaderVisible(true);
		availableDiscipline.setLinesVisible(true);

		Composite composite_1 = new Composite(group, SWT.NONE);
		composite_1.setLayout(new GridLayout(1, false));

		Button addSelected = new Button(composite_1, SWT.NONE);
		addSelected.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				availableDiscipline.getSelection();
				if (availableDiscipline.getSelectionCount() > 0) {
					for (TableItem tableItem : availableDiscipline
							.getSelection()) {
						Discipline discipline = (Discipline) tableItem
								.getData();
						createDisciplineTableItem(discipline,
								selectedDiscipline);
					}
					for (int index : availableDiscipline.getSelectionIndices()) {
						availableDiscipline.remove(index);
					}
				}
			}
		});
		addSelected.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false,
				false, 1, 1));
		addSelected.setText(">");

		Button addAll = new Button(composite_1, SWT.NONE);
		addAll.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				availableDiscipline.removeAll();
				for (Discipline discipline : disciplines) {
					createDisciplineTableItem(discipline, selectedDiscipline);
				}
			}
		});
		addAll.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false,
				1, 1));
		addAll.setText(">>");

		Button removeAll = new Button(composite_1, SWT.NONE);
		removeAll.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				selectedDiscipline.removeAll();
				for (Discipline discipline : disciplines) {
					createDisciplineTableItem(discipline, availableDiscipline);
				}
			}
		});
		removeAll.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false,
				false, 1, 1));
		removeAll.setText("<<");

		Button removeSelected = new Button(composite_1, SWT.NONE);
		removeSelected.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				selectedDiscipline.getSelection();
				if (selectedDiscipline.getSelectionCount() > 0) {
					for (TableItem tableItem : selectedDiscipline
							.getSelection()) {
						Discipline discipline = (Discipline) tableItem
								.getData();
						createDisciplineTableItem(discipline,
								availableDiscipline);
					}
					for (int index : availableDiscipline.getSelectionIndices()) {
						availableDiscipline.remove(index);
					}
				}
			}
		});
		removeSelected.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER,
				false, false, 1, 1));
		removeSelected.setText("<");

		selectedDiscipline = new Table(group, SWT.BORDER | SWT.FULL_SELECTION);
		selectedDiscipline.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				true, 1, 1));
		selectedDiscipline.setHeaderVisible(true);
		selectedDiscipline.setLinesVisible(true);
		this.initData();
	}

	@Override
	protected void okClicked() {
		try {
			ageClassToWorkOn.setCalculationFormula(txtCalculationformula
					.getText());
			ageClassToWorkOn.setName(txtNameofageclass.getText());
			java.util.List<Discipline> disciplines = new ArrayList<Discipline>();
			for (int i = 0; i < selectedDiscipline.getItemCount(); i++) {
				TableItem tableItem = selectedDiscipline.getItem(i);
				Discipline discipline = (Discipline) tableItem.getData();
				disciplines.add(discipline);
			}
			ageClassToWorkOn.setDiscipline(disciplines);

			ageClassController.validate(ageClassToWorkOn);
			ageClassController.createOrUpdate(ageClassToWorkOn);

			close();

		} catch (javax.validation.ConstraintViolationException constraintViolationException) {
			showError(constraintViolationException);
		}
	}
}
